//
//  IniviteFriendViewController.m
//  wtshoppingcite
//
//  Created by TW on 14-10-24.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "IniviteFriendViewController.h"
#import "AppDelegate.h"
#import <MessageUI/MessageUI.h>

@interface IniviteFriendViewController ()<MFMailComposeViewControllerDelegate,MFMessageComposeViewControllerDelegate>

@property(nonatomic,strong)NSString *invitCode;

@end

@implementation IniviteFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self getInviteCode];

    self.view.backgroundColor = [UIColor whiteColor];
    [self setTitleView:@"邀请好友"];
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 74, kWidth-40, 204)];
    imageView.image = [UIImage imageNamed:@"catelog"];
    imageView.userInteractionEnabled = YES;
    [self.view addSubview:imageView];
    

    [imageView addSubview:self.textField];

    
    // Do any additional setup after loading the view.
}

#pragma mark  获取邀请码
-(void)getInviteCode{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    SetValueForKey(params, [RegisterHelper shareHelper].userId, @"userId");
    
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                              andAPISuffix:kInvitCode
                             andHttpMethod:@"POST"
                             andParameters:params
                           andRequestCache:NO
                            andUseParamDic:YES
                       andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                     andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                       
                         if (response.isError) {
                             
                             DLog(@"error %@",response.errorMessage);
                             
                         }else{
                             
                             NSDictionary *responseDic = request.responseJSON;
                             
                             self.invitCode = [responseDic objectForKey:@"registerInviteCode"];
                             
                             DLog(@"成功数据:%@",responseDic);
                             
                           
                         }
                         
                     }];

}

#pragma mark 短信邀请
-(IBAction)duanxinAction:(id)sender{
    
    [self showMessageView];
}

- (void)showMessageView
{
    
    if( [MFMessageComposeViewController canSendText] ){
        
        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init]; //autorelease];
        
        controller.recipients = [NSArray arrayWithObject:@""];
        controller.body =self.textField.text;
        controller.messageComposeDelegate = self;
        
        [self.navigationController presentViewController:controller animated:YES completion:^{ 
        }];
        
        [[[[controller viewControllers] lastObject] navigationItem] setTitle:@""];//修改短信界面标题
    }else{
        
        [self alertWithTitle:@"提示信息" msg:@"设备没有短信功能"];
    }
}

#pragma mark 微信邀请
-(IBAction)weixinAction:(id)sender{
    
    [self sendLinkContent];
    
}


- (void) sendLinkContent
{
    WXMediaMessage *message = [WXMediaMessage message];
    message.title =[NSString stringWithFormat:@"%@:%@", @"零点家园！好玩的社区应用，快来下载吧,我的邀请码是",self.invitCode];
    message.description = @"零点家园！好玩的社区应用，快来下载吧";
    [message setThumbImage:[UIImage imageNamed:@"icon 57.png"]];
    
    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = @"https://itunes.apple.com/us/app/ling-dian-jia-yuan/id954684084?l=zh&ls=1&mt=8";
    
    message.mediaObject = ext;
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneTimeline;
    
    [WXApi sendReq:req];
}


//MFMessageComposeViewControllerDelegate

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    
    [controller dismissViewControllerAnimated:NO completion:^{
    }];//关键的一句   不能为YES
    
    switch ( result ) {
            
        case MessageComposeResultCancelled:
            
            [self alertWithTitle:@"提示信息" msg:@"发送取消"];
            break;
        case MessageComposeResultFailed:// send failed
            [self alertWithTitle:@"提示信息" msg:@"发送成功"];
            break;
        case MessageComposeResultSent:
            [self alertWithTitle:@"提示信息" msg:@"发送失败"];
            break;
        default:
            break;
    }
}


- (void) alertWithTitle:(NSString *)title msg:(NSString *)msg {
    
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:msg
                                                   delegate:self
                                          cancelButtonTitle:nil
                                          otherButtonTitles:@"确定", nil];
    
    [alert show];
    
}


-(IBAction)copyAction:(id)sender{
    
    //self.textField.backgroundColor = UIColorFromRGB(0x008cd6);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    ;
}


- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
